package com.wushi.emeeting.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.wushi.emeeting.model.Document;

public class DocumentDAO {

	public void newDoc(Document document){
		Session session=DAOutil.getSession();
		Transaction trans=session.beginTransaction();
		try{
			session.save(document);
			trans.commit();
		}catch(Exception e){
			e.printStackTrace();
			trans.rollback();
		}finally{
		session.close();
		}
	}
	public List<Document> findDocs(){
		Session session=DAOutil.getSession();
		Query query=session.createQuery("from Doc");
		List<Document> list=(List<Document>)query.list();
		session.close();
		return list;
	}
	
	public Document getDocByID(Long id){
		Session session=DAOutil.getSession();
		Query query=session.createQuery("from Doc as d where d.id=? ");
		query.setLong(0, id);
		List<Document> list=(List<Document>)query.list();
		session.close();
		return list.get(0);
	}
		
	public void saveOrUpdate(Document doc){
		Session session=DAOutil.getSession();
		Transaction trans=session.beginTransaction();
		try{
			session.saveOrUpdate(doc);
		}catch(Exception e){
			e.printStackTrace();
			trans.rollback();
		}finally{
		session.close();
		}
	}
	
	public void deleteDoc(Document doc){
		Session session=DAOutil.getSession();
		Transaction trans=session.beginTransaction();
		try{
			session.delete(doc);
		}catch(Exception e){
			e.printStackTrace();
			trans.rollback();
		}finally{
		session.close();
		}
	}

}
